package com.quan.weixin.wx.web.controller;

import org.n3r.idworker.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.quan.common.core.BaseController;
import com.quan.common.utils.IpHelper;

@RestController
@RequestMapping("/wx/pay")
public class WxPayController extends BaseController {

	@Autowired
	private WxPayService wxService;

	@Autowired
	private IdWorker idWorker;

	/**
	 * 统一下单，并组装所需支付参数
	 * 
	 * @param request
	 * @return
	 * @throws WxPayException
	 */
	@PostMapping("/createOrder")
	public <T> T createOrder(@RequestBody WxPayUnifiedOrderRequest request) throws WxPayException {
		request.setOutTradeNo(idWorker.nextId() + "");
		request.setSpbillCreateIp(IpHelper.getIpAddr(this.request));
		return this.wxService.createOrder(request);
	}

	/**
	 * 支付回调通知处理
	 * 
	 * @param xmlData
	 * @return
	 * @throws WxPayException
	 */
	@PostMapping("/parseOrderNotifyResult")
	public Object parseOrderNotifyResult(@RequestBody String xmlData) throws WxPayException {
		System.out.println("============支付回调通知处理");
		/*
		 * System.out.println(xmlData); WxPayOrderNotifyResult result = this.wxService.parseOrderNotifyResult(xmlData); System.out.println(result.getOutTradeNo()); System.out.println(result.getReturnCode()); System.out.println(result.getReturnMsg()); System.out.println(result.getResultCode());
		 */

		return WxPayNotifyResponse.success("处理成功!");
	}

}
